<?php
/**
 * This file is part of workerman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author    walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link      http://www.workerman.net/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */
namespace Workerman\Protocols;

use Workerman\Connection\ConnectionInterface;

/**
 * Protocol interface
 */
interface ProtocolInterface
{
    /**
     * 此input方法属于分包方法，即对recv_buffer分包
     * Check the integrity of the package.检查包的完整性
     * Please return the length of package.要求返回包的长度
     * If length is unknow please return 0 that mean wating more data.如果此包长度未知，需要return0 ，代表我们需要更多的包数据
     * If the package has something wrong please return false the connection will be closed.
     * 如果 包有其他未知原因，需要返回false，ConnectionInterface的实例的连接（当前客户端连接） 将被关闭
     *
     * @param ConnectionInterface $connection
     * @param string              $recv_buffer
     * @return int|false
     */
    public static function input($recv_buffer, ConnectionInterface $connection);

    /**
     * 请求解包（接收客户端的请求，即有onmessage回调的时候）
     * Decode package and emit onMessage($message) callback, $message is the result that decode returned.
     *
     * 对包解码，（需要满足接收到的数据字节==input的字节，并且接受到了足够的字节 即大于0），则调用此decode方法，同时触发ConnectionInterface的实例的连接 的服务端onMessage回调函数
     * @param ConnectionInterface $connection
     * @param string              $recv_buffer
     * @return mixed
     */
    public static function decode($recv_buffer, ConnectionInterface $connection);




    /**
     *
     * Encode package brefore sending to client.
     * 在发送给客户端时，即调用$connection->send($data)的时候，对包进行打包编码，即发送给客户端时会自动调用,变成符合客户端及服务端一致的协议数据格式
     * @param ConnectionInterface $connection
     * @param mixed               $data
     * @return string
     */
    public static function encode($data, ConnectionInterface $connection);
}
